def set_stage():
""" Sets up the stage for the game """
stage.set_background("soccerfield")
stage.disable_floor()
def add_player():
""" Adds a player to the stage for the user to control """
player = codesters.Sprite("player1")
player.go_to(0, -180)
player.set_size(0.75)
return player
def add_ball():
""" Adds a ball to the stage and sets its attributes """
ball = codesters.Sprite("soccerball")
ball.set_y_speed(-8)
def head_ball(sprite, hit_sprite):
""" Detects collisions between the player and ball """
my_var = hit_sprite.get_y_speed()
hit_sprite.set_y_speed(-my_var + 1)
my_var = hit_sprite.get_x_speed()
hit_sprite.set_x_speed(my_var + 1)
def move_left(sprite):
""" Moves the player left """
sprite.move_left(50)
def main():
""" Sets up the program and calls other functions """
set_stage()
player = add_player()
add_ball()
player.event_collision(head_ball)
main()
t = codesters.Teacher()
defs = t.find_block("def")
key_event_handlers = t.find_text("event_key")
def get_above_def(line_number, def_list):
""" Returns the name of the nearest function and the distance above the given line number """
function_name = ""
distance = line_number - def_list[0][0]
for func in def_list:
if line_number - func[0] <= distance and line_number - func[0] > 0:
function_name = func[1]
distance = line_number - func[0]
return function_name, distance
def find_new_function(ignore_list, def_list):
""" Returns tuples of (line_number, function def) for any functions that aren't specified in ignore list """
return_list = []
for d in def_list:
count = 0
for i in ignore_list:
if i in d[1]:
break
else:
count += 1
if count == len(ignore_list):
return_list.append((d[0],d[1]))
return return_list
try:
tval1 = key_event_handlers[0][1].replace(' ','').lower()
tval1_line_num = key_event_handlers[0][0]
tval1_indent = t.get_indent_at_line(tval1_line_num)
except:
tval1 = "DNE"
tval1_line_num = "DNE"
tval1_indent = "DNE"
try:
above_def, distance = get_above_def(tval1_line_num, defs)
except:
above_def = "DNE"
distance = "DNE"
t1 = TestObjective()
t1.add_success("player." in tval1 and '("left",move_left)' in tval1, "Great job!")
t1.add_failure(tval1 == "DNE", "Did you add a Sprite Key Event Handler to main()?")
t1.add_failure(tval1 != "DNE" and "player." not in tval1, "Oops, make sure the event handler is connecting to player!")
t1.add_failure(tval1 != "DNE" and '"left"' not in tval1, 'Did you replace "right" with "left" in the event handler?')
t1.add_failure(tval1 != "DNE" and "move_left" not in tval1, "Did you replace right_key with move_left in the event handler?")
t2 = TestObjective()
t2.add_success("main" in above_def and tval1_indent == 4, "Great job!")
t2.add_failure("main" not in above_def, "Did you place Sprite Key Event Handler within main()?")
t2.add_failure(tval1_indent < 0 or tval1_indent > 4, "Make sure that the event handler is indented once within main()!")
tester = TestManager()
tester.add_test_list([t1, t2])
tester.run_tests()
tester.display_first_feedback()
-
Run Code
-
Activity Submitted!
Enviar Trabajo
-
Actividad Siguiente
-
Stop Running Code
-
Show Chart
-
Show Console
-
Reset Code Editor
-
Codesters How To (opens in a new tab)